package cn.ylz.ylmall.product.dao.persist.repository.impl;

import cn.ylz.ylmall.product.dao.persist.mapper.CommentLikeMapper;
import cn.ylz.ylmall.product.dao.persist.repository.LikeRecordRepository;
import cn.ylz.ylmall.product.pojo.entity.CommentLike;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LikeRecordRepositoryImpl implements LikeRecordRepository {
    @Autowired
    private CommentLikeMapper commentLikeMapper;

    @Override
    public int insert(CommentLike commentLike) {
        return commentLikeMapper.insert(commentLike);
    }

    @Override
    public boolean existsByCommentIdAndUserId(long commentId, Long userId) {
        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId)
                .eq("user_id", userId);
        int count = commentLikeMapper.selectCount(queryWrapper);
        return count > 0; // 如果记录数量大于0，表示已存在相同的记录
    }

    @Override
    public List<CommentLike> findAll() {
        return commentLikeMapper.selectList(null);
    }

    @Override
    public void delete(CommentLike commentLike) {
        commentLikeMapper.deleteById(commentLike.getId());
    }

    @Override
    public void deleteByCommentId(Long commentId) {
        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId);
        commentLikeMapper.delete(queryWrapper);
    }

}
